package com.iot.dw.netty;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
@Slf4j
public class MyClassLoader extends ClassLoader {
     String filePath="D:\\src\\t1\\iot\\";
    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            // 文件名称
            String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
            String fullName=filePath+fileName;
            FileInputStream fs=new FileInputStream(fullName);
            // 获取文件输入流
            InputStream is = fs;//this.getClass().getResourceAsStream(fullName);
            // 读取字节
            byte[] b = new byte[is.available()];
            is.read(b);

            log.info("***************  read file");

            fs.close();
            // 将byte字节流解析成jvm能够识别的Class对象
            return defineClass(name, b, 0, b.length);
        } catch (Exception e) {
            throw new ClassNotFoundException();
        }

    }
}
